function e(e, t, a) {
    return t in e ? Object.defineProperty(e, t, {
        value: a,
        enumerable: !0,
        configurable: !0,
        writable: !0
    }) : e[t] = a, e;
}

var t, a = function(e) {
    return e && e.__esModule ? e : {
        default: e
    };
}(require("../../utils/config.js")), o = require("../../utils/api.js"), s = require("../../utils/util.js"), n = require("../../utils/auth.js"), i = (require("../../wxParse/wxParse.js"), 
require("../../utils/wxApi.js")), r = require("../../utils/wxRequest.js"), l = getApp();

Page({
    data: (t = {
        userInfo: {},
        readLogs: [],
        topBarItems: [ {
            id: "1",
            name: "浏览",
            selected: !0
        }, {
            id: "2",
            name: "评论",
            selected: !1
        }, {
            id: "3",
            name: "点赞",
            selected: !1
        }, {
            id: "5",
            name: "订阅",
            selected: !1
        } ],
        tab: "1",
        showerror: "none",
        shownodata: "none",
        subscription: ""
    }, e(t, "userInfo", l.globalData.userInfo), e(t, "isLoginPopup", !1), t),
    onLoad: function(e) {
        var t = this;
        l.globalData.isGetOpenid ? t.setData({
            userInfo: l.globalData.userInfo
        }) : t.userAuthorization(), t.fetchPostsData("1");
    },
    onShow: function(e) {},
    redictDetail: function(e) {
        var t = e.currentTarget.id, a = "";
        a = "1" == e.currentTarget.dataset.itemtype ? "../list/list?categoryID=" + t : "../detail/detail?id=" + t, 
        wx.navigateTo({
            url: a
        });
    },
    onTapTag: function(e) {
        for (var t = this, a = e.currentTarget.id, o = t.data.topBarItems, s = 0; s < o.length; s++) a == o[s].id ? o[s].selected = !0 : o[s].selected = !1;
        t.setData({
            topBarItems: o,
            tab: a
        }), 0 !== a ? this.fetchPostsData(a) : this.fetchPostsData("1");
    },
    onShareAppMessage: function() {
        return {
            title: "分享我在“" + a.default.getWebsiteName + "浏览、评论、点赞的文章",
            path: "pages/readlog/readlog",
            success: function(e) {},
            fail: function(e) {}
        };
    },
    fetchPostsData: function(e) {
        var t = this;
        self = this, self.setData({
            showerror: "none",
            shownodata: "none"
        });
        var a = 0;
        if ("1" == e) self.setData({
            readLogs: (wx.getStorageSync("readLogs") || []).map(function(e) {
                return a++, e;
            })
        }), self.setData({
            userInfo: l.globalData.userInfo
        }), 0 == a && self.setData({
            shownodata: "block"
        }); else if ("2" == e) if (self.setData({
            readLogs: []
        }), l.globalData.isGetOpenid) {
            n = l.globalData.openid;
            r.getRequest(o.getWeixinComment(n)).then(function(e) {
                200 == e.statusCode ? (self.setData({
                    readLogs: self.data.readLogs.concat(e.data.data.map(function(e) {
                        return a++, e[0] = e.post_id, e[1] = e.post_title, e;
                    }))
                }), self.setData({
                    userInfo: l.globalData.userInfo
                }), 0 == a && self.setData({
                    shownodata: "block"
                })) : (console.log(e), self.setData({
                    showerror: "block"
                }));
            });
        } else self.userAuthorization(); else if ("3" == e) if (self.setData({
            readLogs: []
        }), l.globalData.isGetOpenid) {
            n = l.globalData.openid;
            r.getRequest(o.getMyLikeUrl(n)).then(function(e) {
                200 == e.statusCode ? (self.setData({
                    readLogs: self.data.readLogs.concat(e.data.data.map(function(e) {
                        return a++, e[0] = e.post_id, e[1] = e.post_title, e[2] = "0", e;
                    }))
                }), self.setData({
                    userInfo: l.globalData.userInfo
                }), 0 == a && self.setData({
                    shownodata: "block"
                })) : (console.log(e), self.setData({
                    showerror: "block"
                }));
            });
        } else self.userAuthorization(); else if ("4" == e) if (self.setData({
            readLogs: []
        }), l.globalData.isGetOpenid) {
            n = l.globalData.openid;
            r.getRequest(o.getMyPraiseUrl(n)).then(function(e) {
                200 == e.statusCode ? (self.setData({
                    readLogs: self.data.readLogs.concat(e.data.data.map(function(e) {
                        return a++, e[0] = e.post_id, e[1] = e.post_title, e[2] = "0", e;
                    }))
                }), self.setData({
                    userInfo: l.globalData.userInfo
                }), 0 == a && self.setData({
                    shownodata: "block"
                })) : (console.log(e), t.setData({
                    showerror: "block"
                }));
            });
        } else self.userAuthorization(); else if ("5" == e) if (self.setData({
            readLogs: []
        }), l.globalData.isGetOpenid) {
            var n = l.globalData.openid, i = o.getSubscription() + "?openid=" + l.globalData.openid;
            r.getRequest(i).then(function(e) {
                if (200 == e.statusCode) {
                    var o = e.data.usermetaList;
                    o && self.setData({
                        readLogs: self.data.readLogs.concat(o.map(function(e) {
                            return a++, e[0] = e.ID, e[1] = e.post_title, e[2] = "0", e;
                        }))
                    }), self.setData({
                        userInfo: l.globalData.userInfo
                    }), 0 == a && self.setData({
                        shownodata: "block"
                    });
                } else console.log(e), t.setData({
                    showerror: "block"
                });
            });
        } else self.userAuthorization(); else "6" == e && (self.setData({
            readLogs: []
        }), r.getRequest(o.getNewComments()).then(function(e) {
            200 == e.statusCode ? (self.setData({
                readLogs: self.data.readLogs.concat(e.data.map(function(e) {
                    return a++, e[0] = e.post, e[1] = s.removeHTML(e.content.rendered + "(" + e.author_name + ")"), 
                    e[2] = "0", e;
                }))
            }), 0 == a && self.setData({
                shownodata: "block"
            })) : (console.log(e), self.setData({
                showerror: "block"
            }));
        }).catch(function() {
            console.log(response), self.setData({
                showerror: "block"
            });
        }));
    },
    userAuthorization: function() {
        var e = this;
        wx.getSetting({
            success: function(t) {
                console.log(t.authSetting);
                var a = t.authSetting;
                "scope.userInfo" in a ? (console.log("不是第一次授权", a), !1 === a["scope.userInfo"] ? wx.showModal({
                    title: "用户未授权",
                    content: "如需正常使用评论、点赞、赞赏等功能需授权获取用户信息。是否在授权管理中选中“用户信息”?",
                    showCancel: !0,
                    cancelColor: "#296fd0",
                    confirmColor: "#296fd0",
                    confirmText: "设置权限",
                    success: function(e) {
                        e.confirm && (console.log("用户点击确定"), wx.openSetting({
                            success: function(e) {
                                console.log("打开设置", e.authSetting), e.authSetting["scope.userInfo"] && n.getUsreInfo(null);
                            }
                        }));
                    }
                }) : n.getUsreInfo(null)) : (console.log("第一次授权"), e.setData({
                    isLoginPopup: !0
                }));
            }
        });
    },
    agreeGetUser: function(e) {
        var t = e.detail.userInfo, a = this;
        t && (n.getUsreInfo(e.detail), a.setData({
            userInfo: t
        })), setTimeout(function() {
            a.setData({
                isLoginPopup: !1
            });
        }, 1200);
    },
    closeLoginPopup: function() {
        this.setData({
            isLoginPopup: !1
        });
    },
    openLoginPopup: function() {
        this.setData({
            isLoginPopup: !0
        });
    },
    confirm: function() {
        this.setData({
            "dialog.hidden": !0,
            "dialog.title": "",
            "dialog.content": ""
        });
    },
    getUsreInfo: function() {
        var e = this, t = "";
        i.wxLogin()().then(function(e) {
            return t = e.code, i.wxGetUserInfo()();
        }).then(function(a) {
            console.log(a.userInfo), console.log("成功获取用户信息(公开信息)"), l.globalData.userInfo = a.userInfo, 
            l.globalData.isGetUserInfo = !0, e.setData({
                userInfo: a.userInfo
            });
            var s = o.getOpenidUrl(), n = {
                js_code: t,
                encryptedData: a.encryptedData,
                iv: a.iv,
                avatarUrl: a.userInfo.avatarUrl
            };
            r.postRequest(s, n).then(function(e) {
                "200" == e.data.status ? (console.log("openid 获取成功"), l.globalData.openid = e.data.openid, 
                l.globalData.isGetOpenid = !0) : console.log(e.data.message);
            });
        }).catch(function(t) {
            console.log("error: " + t.errMsg), e.userAuthorization();
        });
    },
    getSubscription: function() {
        if (l.globalData.isGetOpenid) {
            l.globalData.openid;
            var e = o.getSubscription() + "?openid=" + l.globalData.openid;
            r.getRequest(e).then(function(e) {
                if (200 == e.statusCode && "200" == e.data.status) {
                    var t = e.data.substr;
                    self.setData({
                        subscription: t
                    });
                }
            });
        } else self.userAuthorization();
    }
});